//
//  DBJIllnessTableViewController.m
//  DBJ
//
//  Created by 简楚君 on 2016/11/14.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "DBJIllnessTableViewController.h"
#import <Masonry.h>
#import <MJRefresh.h>
#import <SVProgressHUD.h>
@interface DBJIllnessTableViewController ()

@property(strong,nonatomic)NSMutableDictionary *conDict;

@end

@implementation DBJIllnessTableViewController

-(instancetype)init{
    if (self = [super init]) {
        [DBJTools dbjShowSVTitle:@"拼命加载中..." andTime:1.0];
    }
    return self;
}
-(void)viewDidLoad{
    [super viewDidLoad];
    __weak typeof(self) weakSelf = self;
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //刷新指定时间完成
        [NSThread sleepForTimeInterval:1.3];
        [weakSelf.tableView.mj_header endRefreshing];
        [DBJTools dbjShowSVSuccessTitle:@"加载成功" andTime:0.8];
    }];
    
    //上拉刷新
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //刷新指定时间完成
        [NSThread sleepForTimeInterval:1.3];
        [weakSelf.tableView.mj_footer endRefreshing];
        [DBJTools dbjShowSVSuccessTitle:@"加载成功" andTime:0.8];
    }];
}

/**
 刷新完成方法
 */
//-(void)refreshUpOrDown{
//    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
//    [SVProgressHUD setBackgroundColor:DBJColor];
//    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
//    [SVProgressHUD setMinimumDismissTimeInterval:1.0];
//    [SVProgressHUD showSuccessWithStatus:@"刷新完成"];
//}
-(void)setIllTypes:(NSArray<DBJIllnessListModel *> *)illTypes{
    _illTypes = illTypes;
    self.conDict = [NSMutableDictionary dictionaryWithCapacity:3];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cells"];
    [self.tableView reloadData];
}

-(void)setDidCure:(NSArray *)didCure{
    _didCure = didCure;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cells"];
    [self.tableView reloadData];
}

#pragma mark - 数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.didCure) {
        return self.didCure.count;
    }
    return self.illTypes.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cells" forIndexPath:indexPath];
    //赋值
    if (self.illTypes[indexPath.row].ci3_name) {
        cell.textLabel.text = self.illTypes[indexPath.row].ci3_name;
    }else if(self.didCure){
        cell.textLabel.text = self.didCure[indexPath.row];
    }
    else{
        cell.textLabel.text = self.illTypes[indexPath.row].complication_name;
    }
    //返回cell
    return cell;
}
-(void)viewWillDisappear:(BOOL)animated{
    if ( self.concurrentBlock) {
        self.concurrentBlock(self.conDict);
    }
}
#pragma mark - 代理方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.illTypes[indexPath.row].ci3_name) {
        self.illBlock(self.illTypes[indexPath.row].ci3_name);
        [self.navigationController popViewControllerAnimated:YES];
    }
    if (self.didCure) {
        self.illBlock(self.didCure[indexPath.row]);
        [self.navigationController popViewControllerAnimated:YES];
    }
    if(self.illTypes[indexPath.row].complication_name){
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (!self.conDict[self.illTypes[indexPath.row].complication_name]){
            [self.conDict setValue:self.illTypes[indexPath.row].complication_name forKey:self.illTypes[indexPath.row].complication_name];
            UIButton *btn = [[UIButton alloc]init];
            [btn setImage:[UIImage imageNamed:@"login_protocol"] forState:UIControlStateNormal];
            [cell.contentView addSubview:btn];
            btn.frame = CGRectMake(330, 10, 30, 30);
            
            UIBarButtonItem *navBtn = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(popTolast)];
            self.navigationItem.rightBarButtonItem = navBtn;
            [self.tableView reloadData];
        }else{
            [self.conDict removeObjectForKey:self.illTypes[indexPath.row].complication_name];
            [cell.contentView.subviews.lastObject removeFromSuperview];
            [self.tableView reloadData];
        }
    }
}
-(void)popTolast{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
